package com.xinan.weixin.menu;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.sql.Timestamp;
import java.util.List;
import java.sql.Date;

@TableBind(tableName = "wx_menu")
public class WxMenu extends Model<WxMenu>{
    public static final WxMenu dao = new WxMenu();

    //columns START
    private String ID = "id";  //
    private String NAME = "name";  //标题
    private String TYPE = "type";  //类型
    private String KEY = "key";  //点击必须key
    private String URL = "url";  //view类型必须
    private String PID = "pid";  //上级id
    private String LEVEL = "level";  //级别
    private String YXBZ = "yxbz";  //0无效  1 有效
    private String SXH="sxh";
    public void setId(int value) {
        set(ID, value);
    }

    public int getId() {
        return get(ID);
    }
    public void setName(String value) {
        set(NAME, value);
    }

    public String getName() {
        return get(NAME);
    }
    public void setType(String value) {
        set(TYPE, value);
    }

    public String getType() {
        return get(TYPE);
    }
    public void setKey(String value) {
        set(KEY, value);
    }

    public String getKey() {
        return get(KEY);
    }
    public void setUrl(String value) {
        set(URL, value);
    }

    public String getUrl() {
        return get(URL);
    }
    public void setPid(String value) {
        set(PID, value);
    }

    public String getPid() {
        return get(PID);
    }
    public void setLevel(String value) {
        set(LEVEL, value);
    }

    public String getLevel() {
        return get(LEVEL);
    }
    public void setYxbz(String value) {
        set(YXBZ, value);
    }

    public String getYxbz() {
        return get(YXBZ);
    }

    public String getSXH() {
        return SXH;
    }

    public void setSXH(String SXH) {
        this.SXH = SXH;

    }
    //columns END

    public List<WxMenu> findAll() {
        return find("select * from wx_menu");
    }
    /**
     * @param   where
     * @return  Record
     */
    public List<Record> findAlls(String where) {
        String sqlExp = " from wx_menu";
        if(StringKit.notBlank(where)){
            sqlExp += " where " + where;
        }
        String order =" order by sxh";
        return Db.find( "select * " +sqlExp +order);

    }

    public List<Record> findtree(String where) {
        String order =" order by sxh";
        return  Db.find("select *, pid as pId from wx_menu " + where+order);
    }

    public List<Record> findbymenu(String where) {
        String order =" order by sxh";
        return  Db.find("select *,(select count(*) from wx_menu b where b.pid=a.id) as num from wx_menu a" +
                "" + where+order);
    }

    public Page<WxMenu> page(int pageNumber, int pageSize, String where) {
        String sqlExp = "from wx_menu";
        if(StringKit.notBlank(where)){
            sqlExp += " where " + where;
        }
        return paginate(pageNumber, pageSize, "select *", sqlExp);
    }


}

